package com.hxs.hxsserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxs.hxsserver.domain.dto.device.AlarmQueryDTO;
import com.hxs.hxsserver.domain.dto.device.AlarmRuleDTO;
import com.hxs.hxsserver.domain.entity.device.AlarmLog;
import com.hxs.hxsserver.domain.entity.device.AlarmRule;
import com.hxs.hxsserver.domain.entity.device.Device;
import com.hxs.hxsserver.domain.entity.user.User;
import com.hxs.hxsserver.mapper.device.AlarmLogMapper;
import com.hxs.hxsserver.mapper.device.AlarmRuleMapper;
import com.hxs.hxsserver.service.AlarmService;
import com.hxs.hxsserver.utils.DozerUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class AlarmServiceImpl extends ServiceImpl<AlarmRuleMapper, AlarmRule> implements AlarmService {

    @Resource
    private DozerUtil dozerUtil;
    @Resource
    AlarmRuleMapper alarmRuleMapper;
    @Resource
    AlarmLogMapper alarmLogMapper;

    @Override
    public Boolean insertAlarmRule(AlarmRuleDTO alarmRuleDTO) {
        AlarmRule map = dozerUtil.map(alarmRuleDTO, AlarmRule.class);
        alarmRuleMapper.insert(map);
        return true;
    }

    @Override
    public Boolean updateAlarmRule(AlarmRuleDTO alarmRuleDTO) {
        AlarmRule map = dozerUtil.map(alarmRuleDTO, AlarmRule.class);
        alarmRuleMapper.updateById(map);
        return true;
    }

    @Override
    public Boolean deleteAlarmRule(AlarmRuleDTO alarmRuleDTO) {
        int i = alarmRuleMapper.deleteById(alarmRuleDTO.getId());
        return true;
    }

    @Override
    public List<AlarmRule> getAlarmRuleList(AlarmRuleDTO alarmRuleDTO) {
        LambdaQueryWrapper<AlarmRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlarmRule::getFactorId,alarmRuleDTO.getFactorId());
        List<AlarmRule> list = alarmRuleMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public AlarmLog getAlarmNewLog(User user) {
        LambdaQueryWrapper<AlarmLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlarmLog::getUserId,user.getId());
        queryWrapper.eq(AlarmLog::getStatus,0)
         .orderByDesc(AlarmLog::getCreateTime)
                .last("LIMIT 1");
        AlarmLog alarmLog = alarmLogMapper.selectOne(queryWrapper);
        //将状态更新为已读
        LambdaUpdateWrapper<AlarmLog>  updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AlarmLog::getId,user.getId());
        updateWrapper.set(AlarmLog::getStatus,1);
        alarmLogMapper.update(alarmLog,updateWrapper);
        return alarmLog;
    }

    @Override
    public IPage<AlarmLog> getAlarmLog(AlarmQueryDTO alarmQueryDTO,User user) {
        Page<Device> page = new Page<>(alarmQueryDTO.getPage(), alarmQueryDTO.getSize());
        return alarmLogMapper.getAlarmLog(page,user.getId(),alarmQueryDTO);
    }

    @Override
    public AlarmRule getAlarmRuleById(Integer id) {
        return alarmRuleMapper.selectById(id);
    }
}
